package web.ipcdemo.controllers;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.xml.namespace.QName;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.myBook.slayer.model.MyBook;
import com.myBook.slayer.model.impl.MyBookImpl;


@Controller
@RequestMapping("VIEW")
public class IPCDemoController {
	
	@Autowired
	private MyBook myBook;
	
	public MyBook getMyBook() {
		return myBook;
	}


	public void setMyBook(MyBook myBook) {
		this.myBook = myBook;
	}


	@RenderMapping
	public String handleRenderRequest(Model model, RenderRequest renderRequest){
		 if (!model.containsAttribute("book")) {
	            model.addAttribute("book", myBook);
	        }
		return "view";
	}
	
	
	@ActionMapping
	public void quickAdd(@ModelAttribute("book") MyBookImpl command, BindingResult result,
            SessionStatus status, ActionResponse response,ActionRequest actionRequest) throws IOException, PortletException, PortalException, SystemException {

		System.out.println("Book ==> " + command);
		QName qName  = new QName("http://liferay.com", "myBook", "x");
		response.setEvent(qName, command);
		ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
		response.sendRedirect(PortalUtil.getLayoutURL(themeDisplay));

	}

	
}
